package com.yixun.qfbao.response.premises.detail;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang.StringUtils;

import com.yixun.qfbao.apidto.PremisesDetailResDto;
import com.yixun.qfbao.response.premises.detail.util.CoordinateVoncertUtil;
import com.yixun.qfbao.util.StringUtil;
import com.yixun.qfbao.xf.model.enums.EstateTypeEnum;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author zilong.liu
 * @date 2020-8-12
 * 	楼盘详细信息
 */
@Data
public class DetailInfo implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	public DetailInfo() {
	}

	public DetailInfo(PremisesDetailResDto detailResDto) {
		this.mainPush = Objects.nonNull(detailResDto.getMainPush())?detailResDto.getMainPush():this.mainPush;
		this.premisesName = detailResDto.getName();
		this.estateNames = getEstateNamesByEstateType(detailResDto.getEstateType());
		this.codeNames = StringUtil.getListFromSplitString(detailResDto.getCodeNameCN(), ",");
		List<BigDecimal> priceRange = StringUtil.getMinOrMaxFromSeprateString(detailResDto.getAveragePrice(), ",", 3);
		BigDecimal minPriceRange = null;
		BigDecimal maxPriceRange = null;
		if(priceRange != null) {
			minPriceRange = priceRange.get(0);
			maxPriceRange = priceRange.get(1);
			if(minPriceRange.doubleValue() == maxPriceRange.doubleValue()) {
				this.averagePrice =  minPriceRange.toString();
			}else {
				this.averagePrice =  minPriceRange + "-" + maxPriceRange;
			}
		}
		this.premisesAddress = detailResDto.getPremisesAddress();
		List<BigDecimal> areaRange = StringUtil.getMinOrMaxFromSeprateString(detailResDto.getApartmentArea(), ",", 3);
		BigDecimal minArea = null;
		BigDecimal maxArea = null;
		if(areaRange != null) {
			minArea = areaRange.get(0);
			maxArea = areaRange.get(1);
			if(minArea.doubleValue() == maxArea.doubleValue()) {
				this.area = minArea + "㎡";
			}else {
				this.area = minArea + "-" + maxArea + "㎡";
			}
		}
		// 总价本期不展示
		//BigDecimal minPrice = minArea.multiply(minPriceRange).divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
		//BigDecimal maxPrice = maxArea.multiply(maxPriceRange).divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
		//this.totalPrice = minPrice + "-" + maxPrice + "万";
		List<Integer> layOutInfos = getAndSortApartmentLayout(detailResDto.getApartmentLayout());
		for (int i = 0; i < layOutInfos.size(); i ++) {
			if((i + 1) != layOutInfos.size()) {
				if(i == 0) {
					this.apartmentLayout = (layOutInfos.get(i) + "/");
				}else {
					this.apartmentLayout += (layOutInfos.get(i) + "/");
				}
			}else {
				if(i == 0) {
					this.apartmentLayout = (layOutInfos.get(i) + "室");
				}else {
					this.apartmentLayout += (layOutInfos.get(i) + "室");
				}
			}
		}
		
//		if(layOutInfo.get(0) == layOutInfo.get(1)) {
//			this.apartmentLayout = layOutInfo == null? null: layOutInfo.get(0) + "室";
//		}else {
//			this.apartmentLayout = layOutInfo == null? null: layOutInfo.get(0) + "/" + layOutInfo.get(1) + "室";
//		}
		this.detailBaseInfo = new DetailBaseInfo(detailResDto);
		this.detailCourtInfo = new DetailCourtInfo(detailResDto);
		List<String> coordinate = CoordinateVoncertUtil.coordinateVoncertUtil(1, detailResDto.getLatitude(), detailResDto.getLongitude());
		if(coordinate.size() == 2) {
			this.latitude = coordinate.get(0);
			this.longitude = coordinate.get(1);
		}
	}
	

	@ApiModelProperty(value = "主推标识，具体描述可参考请求的mainPushStatusEnum")
	private int mainPush;
	
	@ApiModelProperty(value = "楼盘名称")
	private String premisesName;
	
	@ApiModelProperty(value = "用途集合")
	private List<String> estateNames;
	
	@ApiModelProperty(value = "标签集合")
	private List<String> codeNames;
	
	/**
	 * 从数据库查出后1，2 ，需要转化成可以直接展示的数据 1-2
	 */
	@ApiModelProperty(value = "均价")
	private String averagePrice;
	
	/**
	 * 最小价格*最小面积 - 最大价格*最大面积
	 */
	@ApiModelProperty(value = "总价")
	private String totalPrice;
	
	/**
	 * 取户型的最大和最小值范围
	 */
	@ApiModelProperty(value = "面积")
	private String area;
	
	/**
	 *需要根据户型截取出最大值和最小值，然后拼接
	 */
	@ApiModelProperty(value = "户型")
	private String apartmentLayout;
	
	@ApiModelProperty(value = "楼盘地址")
	private String premisesAddress;
	
	@ApiModelProperty(value = "经度，可用于后期定位")
	private String latitude;
	
	@ApiModelProperty(value = "纬度，可用于后期定位")
	private String longitude;

	@ApiModelProperty(value = "点击详情进去查看的基本信息")
	private DetailBaseInfo detailBaseInfo;
	
	@ApiModelProperty(value = "点击详情进去查看的户型信息")
	private DetailCourtInfo detailCourtInfo;
	
	
	/**
	 * 根据使用逗号拼接的多个户型字符串截取卧室数量信息，并取得最大和最小值
	 * @param allLayoutInfo
	 * @return
	 */
//	private List<Integer> getMinAndMaxLayoutInfo(String allLayoutInfo) {
//		if(StringUtils.isBlank(allLayoutInfo)) {
//			return null;
//		}
//		String[] allLayoutInfos = allLayoutInfo.split(",");
//		int max = 0;
//		int min = 0;
//		for(int i = 0, j = allLayoutInfos.length; i < j; i ++) {
//			int number = getRoomNumberByApartmentLayout(allLayoutInfos[i]);
//			if(i == 0) {
//				max = min = number;
//			}
//			if(number < min) {
//				min = number;
//			}
//			if(number > max) {
//				max = number;
//			}
//		}
//		List<Integer> minAndMaxList = new ArrayList<>();
//		minAndMaxList.add(min);
//		minAndMaxList.add(max);
//		return minAndMaxList;
//	}
	
	private static List<Integer> getAndSortApartmentLayout(String allApartmentLayout) {
		String[] allLayoutInfos = allApartmentLayout.split(",");
		List<Integer> rommInfos = new ArrayList<>();
		for(int i = 0, j = allLayoutInfos.length; i < j; i ++) {
			int rooNumber = getRoomNumberByApartmentLayout(allLayoutInfos[i]);
			if(!rommInfos.contains(rooNumber)) {
				rommInfos.add(rooNumber);
			}
		}
		Collections.sort(rommInfos);
		return rommInfos;
	}
	
	
	private static int getRoomNumberByApartmentLayout(String apartmentLayout) {
		String numberStr = apartmentLayout.substring(0, apartmentLayout.indexOf("室"));
		if("无卧".equals(numberStr)) {
			return 0;
		}
		return Integer.parseInt(numberStr);
	}
	
	private List<String> getEstateNamesByEstateType(String estateType) {
		List<String> estateNames = new ArrayList<>();
		if(StringUtils.isNotBlank(estateType)){
			String[] estates = estateType.split(",");
			for (String estate : estates) {
				estateNames.add(EstateTypeEnum.getByNo(estate).getDes());
			}
		}
		return estateNames;
	}
}
